package com.xhwl.centiotdata.controller;



import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.enums.pedestrian.PedestrianMethodEnum;
import com.xhwl.centiotdata.pollingtrigger.pedestrian.PedestrianDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.PedestrianIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.dto.auth.PedestrianAuthDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Collections;
import java.util.Date;
import java.util.List;


/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 15:52 2022/1/26
 */
@RestController
public class PedestrianController {

    @Autowired
    PedestrianDispatcher pedestrianDispatcher;
    @Autowired
    private PedestrianIntegrationConfig integrationConfig;

    @GetMapping("/pedestrian/pedestrian_snap_user")
    public void getRecord(@RequestParam("intervalDays") Integer intervalDays, @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        //人行获取出入记录
        ThreadPoolUtils.pedestrianExecutor.execute(() ->  pedestrianDispatcher.executePullPedestrianRecord(intervalDays,date));
    }
    @GetMapping(value = "/pedestrian/pull-clock-record")
    public void pullClockRecord(Integer offSet, Integer limit) {
        // 人行获取考勤打卡记录
        ThreadPoolUtils.executor.execute(() -> {
            pedestrianDispatcher.executePullClockRecord(offSet,limit);
        });
    }

    @GetMapping(value = "/pedestrian/pull-check-data")
    public void pullCheckData() {
        // 拉取第三方金茂校园对接数字哨兵的查验记录
        pedestrianDispatcher.executePullCheckData();
    }

    @GetMapping(value = "/pedestrian/pull-visitor-data")
    public void pullVisitorData(@RequestParam("intervalDays") Integer intervalDays) {
        pedestrianDispatcher.executePullVisitorData(intervalDays);
    }

    /**
     * 中建物业项目手动同步第三方子系统权限至兴海
     *
     * @param pedestrianAuthDTO param
     */
    @PostMapping(value = "/pedestrian/pull-third-permission")
    public void pullPedestrianThirdPermission(@RequestBody PedestrianAuthDTO pedestrianAuthDTO) {
        ThreadPoolUtils.pedestrianExecutor.execute(() -> {
            List<PedestrianIntegrationConfigDTO> integrationConfigDTOList = integrationConfig
                    .getIntegrationConfig(null)
                    .getOrDefault(PedestrianMethodEnum.RECORD.getId(), Collections.emptyList());

            integrationConfigDTOList.forEach(integrationConfigDTO -> {
                if (integrationConfigDTO.getProjectId().equals(pedestrianAuthDTO.getProjectId())) {
                    PedestrianIntegrationHandler handler = PedestrianIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
                    if (handler == null) {
                        return;
                    }
                    handler.syncThirdAuthorizedDoorInfo(pedestrianAuthDTO, integrationConfigDTO);
                }
            });

        });
    }

}
